<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket</title>
</head>
<body>
<div style="text-align: center">
    <strong style="color: red" id="msg">
    </strong>
</div>
</body>
<script src="http://mat1.gtimg.com/libs/jquery2/2.2.0/jquery.js"></script>
<script type="text/javascript">
    let number = 0;
    var socket = new WebSocket("ws://www.easyswoole.cn:9501/?token=987654321");
    // 打开Socket
    socket.onopen = function (event) {
        setTimeout(function () {
            console.log(1);
            // 发送一个初始化消息
            socket.send('{"class":"WebSocketMessage","action":"run","content":"test"},tttt');
        }, 1);

        // 监听消息
        socket.onmessage = function (event) {
            // $('#msg').empty();
            $('#msg').append('<p>' + event.data + '</p>');
        };
        // 监听Socket的关闭
        socket.onclose = function (event) {
            alert('后端已经关闭')
        };
    };

    window.setInterval(function () {
        number += 1;
        socket.send('{"class":"WebSocketMessage","action":"run","content":[{"name":"test","number":' + number + '}]}')
    }, 1000);

</script>
</html>
